package com.zxh.dubbozxh.framework.register;

import com.zxh.dubbozxh.framework.URL;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Redis,ZooKeeper
 */
public class RemoteMapRegister {

    private static final Map<String, List<URL>> REGISTER = new HashMap<>(16);

    public static void register(String interfaceName, URL url) {
        List<URL> list = REGISTER.get(interfaceName);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(url);

        REGISTER.put(interfaceName, list);

        saveFile();
    }

    public static List<URL> get(String interfaceName) {
        Map<String, List<URL>> REGISTER = getFile();
        return REGISTER.get(interfaceName);
    }

    private static void saveFile(){
        try{
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\temp\\temp.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(REGISTER);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    private static Map<String, List<URL>> getFile() {
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\temp\\temp.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            return (Map<String, List<URL>>) objectInputStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
